package com.test.javafaq_search.controller;

import com.github.pagehelper.PageInfo;
import com.test.javafaq_commons.vo.R;
import com.test.javafaq_search.service.IQuestionService;
import com.test.javafaq_search.vo.QuestionVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
@RequestMapping("/v1/questions")
public class QuestionController {

    @Resource
    IQuestionService questionService;


    @PostMapping
    public R<PageInfo<QuestionVo>> search(String keyWord, Integer pageNum, @AuthenticationPrincipal UserDetails user){

        if(keyWord == null){
            keyWord = "";
        }
        if(pageNum == null){
            pageNum = 1;
        }
        Integer pageSize = 3;
        PageInfo<QuestionVo> pageInfo = questionService.search(keyWord, user.getUsername(), pageNum, pageSize);

        return R.ok(pageInfo);
    }

}
